package cn.regexp.demo.action;

import cn.hutool.core.date.DateTime;
import cn.regexp.demo.entity.Discussion;
import cn.regexp.demo.entity.Problem;
import cn.regexp.demo.ui.ProblemPanel;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Regexpei
 * @date 2024/6/13 0:02
 * @description
 */
public class ProblemDiscussionAction extends AnAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());

        List<Problem> problemList = initData();
        for (int i = 0; i < problemList.size(); i++) {
            ProblemPanel problemPanel = new ProblemPanel(problemList.get(i));

            GridBagConstraints constraints = new GridBagConstraints(0, i, 1, 1, 1.0,
                    0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, JBUI.insets(2), 0, 4);
            mainPanel.add(problemPanel.getMainPanel(), constraints);
        }

        JDialog dialog = new JDialog();
        dialog.setTitle("Problem Discussion");
        // 将 JPanel 设置为 JDialog 的内容面板
        dialog.setContentPane(mainPanel);
        // 设置大小
        dialog.setSize(300, 200);
        // 设置位置为屏幕中心
        dialog.setLocationRelativeTo(null);
        // 设置为模态对话框，阻止与其他窗口交互
        dialog.setModal(true);
        // 设置关闭操作
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        // 显示 JDialog
        dialog.setVisible(true);
    }

    public static List<Problem> initData() {

        List<Discussion> discussionList = new ArrayList<>();
        discussionList.add(new Discussion("小一", "往死里学", DateTime.now()));
        discussionList.add(new Discussion("小二", "往活里学", DateTime.now()));
        discussionList.add(new Discussion("小三", "不学", DateTime.now()));

        return Collections.singletonList(new Problem("题目1", "如何学习Java", DateTime.now(), discussionList));
    }

}
